# run in master

node01_host="node01"
node02_host="node02"

################
# you should do first
################
# install mysql
# create mysql user 'slurm' 👇
# $ wget https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm
# $ yum install mysql80-community-release-el7-7.noarch.rpm
# $ yum install mysql-community-server
# >>> CREATE USER 'slurm'@'localhost' IDENTIFIED WITH mysql_native_password BY '*****';
# >>> GRANT ALL ON *.* TO 'slurm'@'localhost';
# >>> FLUSH PRIVILEGES;
# $ systemctl restart mysqld

echo "The script did not complete!"
exit -1

# create user
export MUNGEUSER=1001
groupadd -g $MUNGEUSER munge
useradd  -m -c "MUNGE Uid 'N' Gid Emporium" -d /var/lib/munge -u $MUNGEUSER -g munge  -s /sbin/nologin munge

export SLURMUSER=1002
groupadd -g $SLURMUSER slurm
useradd  -m -c "SLURM workload manager" -d /var/lib/slurm -u $SLURMUSER -g slurm  -s /bin/bash slurm

#############
# --- munge 
#############
yum install -y epel-release
yum install -y munge munge-libs munge-devel
yum install -y rng-tools

rngd -r /dev/urandom
/usr/sbin/create-munge-key -r

dd if=/dev/urandom bs=1 count=1024 > /etc/munge/munge.key
chown munge: /etc/munge/munge.key
chmod 400 /etc/munge/munge.key

scp /etc/munge/munge.key root@$node01_host:/etc/munge
scp /etc/munge/munge.key root@$node02_host:/etc/munge

# master
chown -R munge: /etc/munge/ /var/log/munge/
chmod 0700 /etc/munge/ /var/log/munge/
systemctl enable munge
systemctl start munge

# node01
expect <<EOF
        set timeout 100
        ssh root@$node01_host
        groupadd -g $MUNGEUSER munge
        useradd  -m -c "MUNGE Uid 'N' Gid Emporium" -d /var/lib/munge -u $MUNGEUSER -g munge  -s /sbin/nologin munge
        groupadd -g $SLURMUSER slurm
        useradd  -m -c "SLURM workload manager" -d /var/lib/slurm -u $SLURMUSER -g slurm  -s /bin/bash slurm
        yum install -y epel-release
        yum install -y munge munge-libs munge-devel
        chown -R munge: /etc/munge/ /var/log/munge/
        chmod 0700 /etc/munge/ /var/log/munge/
        systemctl enable munge
        systemctl start munge
        exit
EOF

# node02
expect <<EOF
        set timeout 100
        ssh root@$node02_host
        groupadd -g $MUNGEUSER munge
        useradd  -m -c "MUNGE Uid 'N' Gid Emporium" -d /var/lib/munge -u $MUNGEUSER -g munge  -s /sbin/nologin munge
        groupadd -g $SLURMUSER slurm
        useradd  -m -c "SLURM workload manager" -d /var/lib/slurm -u $SLURMUSER -g slurm  -s /bin/bash slurm
        yum install -y epel-release
        yum install -y munge munge-libs munge-devel
        chown -R munge: /etc/munge/ /var/log/munge/
        chmod 0700 /etc/munge/ /var/log/munge/
        systemctl enable munge
        systemctl start munge
        exit
EOF

##############
# --- slurm
##############

yum install -y openssl openssl-devel pam-devel numactl numactl-devel hwloc hwloc-devel lua lua-devel readline-devel rrdtool-devel ncurses-devel man2html libibmad libibumad -y
yum install -y perl-ExtUtils-MakeMaker
yum install -y gcc

yum install -y wget
yum install -y rpm-build
yum install -y python3

mkdir /root/slurm_sour && cd /root/slurm_sour
wget https://download.schedmd.com/slurm/slurm-23.02.0.tar.bz2
rpmbuild -ta slurm-23.02.0.tar.bz2

cd /root/rpmbuild/RPMS/x86_64/
yum -y install slurm-*.rpm

# node01
ssh root@$node01_host "mkdir -p /root/slurm_rpm"
scp /root/rpmbuild/RPMS/x86_64/slurm-*.rpm root@$node01_host:/root/slurm_rpm

# node02
ssh root@$node02_host "mkdir -p /root/slurm_rpm"
scp /root/rpmbuild/RPMS/x86_64/slurm-*.rpm root@$node02_host:/root/slurm_rpm

# chmod -- 
chown slurm.slurm /etc/slurm/slurmdbd.conf
chmod 600 /etc/slurm/slurmdbd.conf
chown root /etc/slurm/slurm.conf
mkdir /var/spool/slurmctld
chown slurm.slurm /var/spool/slurmctld

# make log dir ?


